$(document).on('click', '.j_finish_task', function() {
  var that = $(this);
  that.button('loading');
  
  var id = that.attr('data-user-task-id');
  jQuery.ajax({
    url: contextPath + '/task-finish.action?struts.enableJSONValidation=true',
    type: 'POST',
    dataType: 'json',
    data: {'userTask.id':id},
    complete: function(xhr, textStatus) {
      that.button('reset');
    },
    success: function(data, textStatus, xhr) {
      that.button('reset');
      if (data['status'] === -1) {
        alert('您需要先登录才能进行操作');
        window.location.href = contextPath + "/login-index.action?redirectUrl="+encodeURIComponent(window.location.href);
      }
      if(data['fieldErrors'] != null) {
        for (var i in data['fieldErrors']) {
          alert(data['fieldErrors'][i]);
        }
        return;
      }
      if(data['errors'] != null) {
        for (var i in data['errors']) {
          alert(data['errors'][i]);
        }
        return;
      }
      if (data['status'] === 1) {
        alert('确认成功');
        window.location.reload();
      } else {
        alert(data.message);
      }
    },
    error: function(xhr, textStatus, errorThrown) {
      console.log(xhr, textStatus, errorThrown);
    }
  });

  return false;
});
